package com.magicsoft.share.ui.my;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.magicsoft.share.R;
import com.magicsoft.share.adapter.my.AddressAdapter;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.Address;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.presenter.my.AddressPresenter;
import com.magicsoft.share.utils.DensityUtils;
import com.magicsoft.share.view.my.IAddressView;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AddressActivity extends BaseActivity<IAddressView, AddressPresenter> implements IAddressView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.btn_add)
    Button btnAdd;
    private AddressAdapter addressAdapter;

    @Override
    public AddressPresenter initPresenter() {
        return new AddressPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_address;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        tvTitle.setText("地址管理");
        initRV();

    }

    @Override
    public void requestData() {

        mPresenter.addressList();
    }


    @OnClick({R.id.ibtn_left, R.id.btn_add})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.btn_add:
                Intent intent = new Intent(this, AddressDetailActivity.class);
                intent.putExtra("title", "新增收货地址");
                startActivityForResult(intent,Constant.REFRESH_UI);
                break;
        }
    }

    private void initRV() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DefaultItemDecoration(getResources().getColor(R.color.bg2), 0, DensityUtils.dp2px(this, 10f)));       /* recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));*/

        addressAdapter = new AddressAdapter(R.layout.item_address);
        addressAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_edit) {

                    Intent intent = new Intent(AddressActivity.this, AddressDetailActivity.class);
                    intent.putExtra("addressId",addressAdapter.getItem(position).getId());
                    intent.putExtra("address",addressAdapter.getItem(position));
                    intent.putExtra("title", "修改收货地址");
                    startActivityForResult(intent, Constant.REFRESH_UI);
                } else if (view.getId() == R.id.tv_delete) {
                   mPresenter.deleteAddress(addressAdapter.getItem(position).getId());

                }else if (view.getId()==R.id.radio){
                    mPresenter.saveAddress(addressAdapter.getItem(position).getId(),1,null,null,null,null,null);
                }
            }
        });
        addressAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                Address item = addressAdapter.getItem(position);
//                Intent intent = new Intent();
//                intent.putExtra("address",item);
//                setResult(RESULT_OK,intent);
//                finish();
            }
        });
        recyclerView.setAdapter(addressAdapter);
    }

    @Override
    public void addressListSuccess(List<Address> list) {
        addressAdapter.setNewData(list);

    }

    @Override
    public void saveAddressSuccess() {
        requestData();
    }
}
